package com.spring.demo.init;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import com.spring.demo.core.entity.User;
import com.spring.demo.init.aop.AopConfig;
import com.spring.demo.init.db.JdbcTemplateDatabaseConfig;
import com.spring.demo.init.db.JpaDatabaseConfig;
import com.spring.demo.init.httpinvoker.HttpServerConfig;
import com.spring.demo.init.oxm.OxmConfig;
import com.spring.demo.init.rmi.RmiServerConfig;

@Configuration
@ComponentScan({ "com.spring.demo.core" })
@Import({ RmiServerConfig.class, HttpServerConfig.class, AopConfig.class,
		JpaDatabaseConfig.class, OxmConfig.class,
		JdbcTemplateDatabaseConfig.class })
@EnableAsync
public class AppConfig {

	@Bean(name = "userMap")
	public Map<Long, User> userMap() {
		return new ConcurrentHashMap<Long, User>();
	}

	@Bean
	public AnnotationAwareAspectJAutoProxyCreator serviceProxyFactory() {
		List<String> aspectBeanNames = new ArrayList<String>();
		aspectBeanNames.add("serviceLogAspect");
		AnnotationAwareAspectJAutoProxyCreator proxyFactory = new AnnotationAwareAspectJAutoProxyCreator();
		proxyFactory.setIncludePatterns(aspectBeanNames);

		return proxyFactory;
	}

	@Bean
	public TaskExecutor taskExecutor() {
		ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
		taskExecutor.setMaxPoolSize(10);
		taskExecutor.setCorePoolSize(10);
		taskExecutor.setThreadNamePrefix("Spring-Async");

		return taskExecutor;
	}
}
